/**
 * Created by 白雨浓 on 17-8-10 下午2:55.
 *
 * 拦截器
 */
import router from '../router'

const DEFAULT_PATH = "/login";

const Filter = {
  install(Vue){
    /** 修改title*/
    const changeTitle = (to) => {
      if (to.meta.title !== undefined)
        document.title = to.meta.title;
    };

    /** 拦截器*/
    const filter = (to, next) => {
      start();

      if (to.meta.filter) {
        Vue.prototype.$$get(null)
          .then(res => {
            if (res.data.code === 1)
              next();
            else
              next(DEFAULT_PATH);
          })
          .catch(err => {
            Vue.nextTick(() => {
              finish();
            });
            next(false);
          });
      } else {
        next();
      }
    };

    /** 进度条开始*/
    const start = () => {
    };

    /** 进度条结束*/
    const finish = () => {
    };

    router.beforeEach((to, from, next) => {
      changeTitle(to);
      filter(to, next);
    });

    router.afterEach(() => {
      Vue.nextTick(() => {
        finish();
      });
    })
  }
};

export default Filter;
